//
//  BBCustomViewController.m
//  BearSiX
//
//  Created by BearBaby on 16/3/26.
//  Copyright © 2016年 Tom. All rights reserved.
//

#import "BBCustomViewController.h"
#import "BBCustomCell.h"
@interface BBCustomViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)UITableView* tableView;
@end

@implementation BBCustomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"自定义申请";
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WINSIZEWIDTH, WINSIZEHEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  
        return 1;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* identifire = @"Cell";
    BBCustomCell* cell = [self.tableView dequeueReusableCellWithIdentifier:identifire];
    if (indexPath.section==0) {
        
        if (!cell) {
            cell = [[BBCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifire];
            cell.delegate = self;
            cell.textLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/25];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.addImage addTarget:self action:@selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
        }
    }else if(indexPath.section==1){
        
        UITableViewCell* cell1 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        
        cell1.textLabel.text = @"标题";
      cell1.detailTextLabel.text = @"请输入标题";
        
        cell1.textLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/25];
        
        
        
        
        return cell1;
        
        
        
    }else if (indexPath.section==2){
        UITableViewCell* cell2 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        cell2.textLabel.text = @"提交到";
        cell2.textLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/25];
        
        cell2.detailTextLabel.text = @"加班";
        
        
        return cell2;
        
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        CGFloat cellHeight = 0.0f;
        BBCustomCell* cell = (BBCustomCell*)[self tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath];
        cellHeight = cell.finalH;
        return cellHeight;
    }else
        return 44;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  
    return 0.000000001;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
 
        return 15;
    
    
}

//添加图片
-(void)addImage:(UIButton*)sender{
    
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {

        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"aaa" message:@"nn" preferredStyle:(UIAlertControllerStyleActionSheet)];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"hello world");
        }];
        
        _sheet  = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择"  , nil];
    }
    else
    {
        _sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消"  destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];
    }
    
    _sheet.tag = 255;
    
    [_sheet showInView:self.view];
    
    
}
#pragma mark - actionsheet delegate
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 255)
    {
        NSUInteger sourceType = 0;
        
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            switch (buttonIndex)
            {
                case 0:// 相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:// 相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                case 2:
                    return;
                    break;
            }
        }
        else
        {
            if (buttonIndex == 0)
            {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
            else
            {
                return;
            }
        }
        
        if (sourceType == UIImagePickerControllerSourceTypePhotoLibrary || sourceType ==UIImagePickerControllerSourceTypeSavedPhotosAlbum)
        {
            
            
        }
        else if (sourceType == UIImagePickerControllerSourceTypeCamera)
        {
            // 跳转到相机或相册页面
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            imagePickerController.delegate = self;
            imagePickerController.allowsEditing = YES;
            imagePickerController.sourceType = sourceType;
            [self presentViewController:imagePickerController animated:YES completion:^{}];
        }
    }
}
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // End the context
    UIGraphicsEndImageContext();
    
    // Return the new image.
    return newImage;
}


//完成
-(void)done:(UIBarButtonItem*)item{



}
@end
